package com.xsp.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.xsp.eduservice.entity.EduSubject;
import com.xsp.eduservice.entity.excel.ExcelSubjectData;
import com.xsp.eduservice.entity.subject.OneSubject;
import com.xsp.eduservice.entity.subject.TwoSubject;
import com.xsp.eduservice.listener.SubjectExcelListener;
import com.xsp.eduservice.mapper.EduSubjectMapper;
import com.xsp.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-08
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类对象
    @Override
    public void importSubjectData(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //拿到文件流
            InputStream inputStream = file.getInputStream();
            //调用EasyExcel读数据的方法，放入输入流，放入保存列的对象，放入监听器（传入eduSubjectService）然后调用sheet()方法，调用读的方法
            //此方式最后会自动关流，方式二需要手动关流
            EasyExcel.read(inputStream,ExcelSubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
//            方式二：
//            ExcelReader excelReader = EasyExcel.read(inputStream,ExcelSubjectData.class,new SubjectExcelListener(eduSubjectService)).build();
//            ReadSheet readSheet = EasyExcel.readSheet().build();
//            excelReader.read(readSheet);
//            excelReader.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取所有课程分类列表(树形)
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        //parent_id等于0的是一级分类
        wrapperOne.eq("parent_id","0");
        //这里使用的是mybatisPlus,本类继承了ServiceImpl,里面已注入baseMapper,因此这里不需要注入mapper
        //查询该wrapper条件,返回列表
        List<EduSubject> oneSubject = baseMapper.selectList(wrapperOne);

        //查询二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        //parent_id不等于0的是二级分类
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubject = baseMapper.selectList(wrapperTwo);

        //创建list集合，存储最终数据；由于一级分类包含了二级分类，因此泛型设为一级分类即可
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //封装一级分类
        //遍历一级分类list
        for (EduSubject subjectOne:oneSubject) {
            //二级分类list，用来放入一级分类中
            List<TwoSubject> twoSubjects = new ArrayList<>();
            //一级分类实体类
            OneSubject subject1 = new OneSubject();
            //实体类存入数据
            //subject1.setId(subjectOne.getId());
            //subject1.setTitle(subjectOne.getTitle());
            //以上代码可以用BeanUtils的copy...优化
            //copyProperties实际上是将args1的get赋值给args2的set(参数类型和名称一样的情况下)
            //因此这里会自动将subjectOne中的id和title赋值给subject1中的id和title
            BeanUtils.copyProperties(subjectOne,subject1);

            //封装二级分类
            //遍历二级分类list
            for (EduSubject subjectTwo:twoSubject) {
                //如果二级分类的父id等于一级分类的id,则说明属于该一级分类
                if (subjectTwo.getParentId().equals(subjectOne.getId())){
                    //原本在这里创建二级分类集合,但由于作用域,挪到了上面 List<TwoSubject> twoSubjects = new ArrayList<>();
                    //二级分类实体类
                    TwoSubject subject2 = new TwoSubject();
                    //实体类存入数据
                    //subject2.setId(subjectTwo.getId());
                    //subject2.setTitle(subjectTwo.getTitle());
                    BeanUtils.copyProperties(subjectTwo,subject2);
                    //将二级分类实体类放入到二级分类集合list中
                    twoSubjects.add(subject2);
                }
            }
            //设置一级分类中children(二级分类)属性
            subject1.setChildren(twoSubjects);
            //将一级分类实体类最终的数据存入到finalSubjectList中
            //这里在foreach中,会不断将一级分类添加到finalSubjectList中
            finalSubjectList.add(subject1);
        }
        return finalSubjectList;
    }
}
